from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class Article(db.Model):
    """文章模型"""
    __tablename__ = 'articles'
    
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    digest = db.Column(db.Text)
    content_url = db.Column(db.String(500), unique=True, nullable=False)
    content = db.Column(db.Text)
    cover = db.Column(db.String(500))
    create_time = db.Column(db.DateTime, default=datetime.now)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    author = db.Column(db.String(100))
    copyright_stat = db.Column(db.Integer, default=0)
    pdf_path = db.Column(db.String(500))
    
    def __repr__(self):
        return f'<Article {self.title}>'
        
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'title': self.title,
            'digest': self.digest,
            'content_url': self.content_url,
            'cover': self.cover,
            'create_time': self.create_time.isoformat(),
            'update_time': self.update_time.isoformat(),
            'author': self.author,
            'copyright_stat': self.copyright_stat,
            'pdf_path': self.pdf_path
        } 